<?php

namespace app\admin\service;

use app\admin\model\Libraryopen;

class Library
{

    /**
     * 保存公开人员
     */
    public static function saveLibraryopen($params){
        if(!$params){
            return ['code' => 0, 'msg' => '参数错误'];
        }

        $libraryId = $params['id'];
        $model = new Libraryopen();

        if($params['openType'] == 1) {
            $res = $model->where('libraryId', $libraryId)->delete();
            if($res === false){
                return ['code' => 0, 'msg' => $model->getError()];
            }

            $memberIds = explode(',', $params['openmemberIds']);
            $memberIds = array_unique($memberIds);
            if($memberIds){
                foreach ($memberIds as $memberId) {
                    $result = $model->insert(['libraryId' => $libraryId, 'memberId' => $memberId]);
                }
            }
        } else {
            $result = $model->where('libraryId', $libraryId)->delete();
            $result = true;
        }

        if ($result !== false) {
            return ['code' => 1, 'msg' => '保存成功'];
        } else {
            return ['code' => 0, 'msg' => $model->getError()];
        }
    }

}